

function checkExecutableSkill_Testament(obj)  
{
	if (!obj) return false;

	local isUseSkill = obj.sq_IsUseSkill(SKILL_TESTAMENT);
	if (isUseSkill) 
	{

		obj.getVar("GLOBAL_PARAMETER_CANCEL").set_vector(0,0);
		local pIntVec = sq_GetGlobalIntVector();
		sq_IntVectorClear(pIntVec);
		sq_IntVectorPush(pIntVec, 0);			
		obj.addSetStatePacket(STATE_TESTAMENT , pIntVec,  STATE_PRIORITY_USER, false, "");

		return true;
	}
	return false;
}

function checkCommandEnable_Testament(obj)
{


	return true;
}


function preLoad_TestMent(obj)
{
	local level = sq_GetSkillLevel(obj, SKILL_TESTAMENT);
	if (level > 0)
	{

		createFristAnimationPooledObject(obj,
		"character/swordman/effect/animation/attestament/testamentbackground_00.ani");
		createFristAnimationPooledObject(obj,
		"character/swordman/effect/animation/attestament/testamentendcover_06.ani");
		createFristAnimationPooledObject(obj,
		"character/swordman/effect/animation/attestament/testamentfrontb_00.ani");
		createFristAnimationPooledObject(obj,
		"character/swordman/effect/animation/attestament/testamentstart_bodyeffect.ani");

	}
}

function onSetState_TestMent(obj, state, datas, isResetTimer)
{
	obj.sq_StopMove();
	local state = obj.sq_GetVectorData(datas, 0);
	obj.setSkillSubState(state);
	if (state == 0)
	{
		

		obj.getVar("soundRandom").clear_vector();
		obj.getVar("soundRandom").push_vector( sq_getRandom(0,100).tointeger() );

		if (obj.getVar("soundRandom").get_vector(0) < 50)
			playSoundTime(obj,"SW_TESTAMENT_01_A",100);
		else
			playSoundTime(obj,"SW_TESTAMENT_01_B",100);


		obj.getVar("pooledObj").clear_obj_vector();
		obj.getVar("pooledObjToCover").clear_obj_vector();

		if(!sq_IsMyControlObject(obj) )
		{
			setAniByFile(obj,
			"character/swordman/atanimation/char/testamentstart_body.ani");
		}else
		{
			setAniByFile(obj,
			"character/swordman/atanimation/char/testamentstart_body.ani");
		}

		CreateTestaMentStartBodyEffect(obj,0,1,0);
		sq_SetCharacterNameHide(obj,true);
		
	}else if (state == 1)
	{
		
		local objectManager = obj.getObjectManager();
		local xPos = objectManager.getFieldXPos(400, ENUM_DRAWLAYER_NORMAL);
		local yPos = objectManager.getFieldYPos(400, 0, ENUM_DRAWLAYER_NORMAL);
		obj.sq_SetfindNearLinearMovablePos(xPos,yPos,obj.getXPos(), obj.getYPos(), 10);
		
		
		CreateTestaMentBackGroundEffect(obj,0,1,0);	
		CreateTestaMentFrontBEffect(obj,0,1,0);
		sq_ChangeDrawLayer(obj, ENUM_DRAWLAYER_COVER);

		if(!sq_IsMyControlObject(obj) )
		{
			setAniByFile(obj,
			"character/swordman/atanimation/char/testamentfornta_body_2.ani");
		}else
		{
			setAniByFile(obj,
			"character/swordman/atanimation/char/testamentfronta_body.ani");
		}


		
		//CreateTestaMentBodyCoverEffect(obj,0,-1,0);
		//
		obj.getVar("flag").clear_vector();
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);//5
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);
		obj.getVar("flag").push_vector(0);

		
	}else if (state == 2)
	{
		sq_ChangeDrawLayer(obj, ENUM_DRAWLAYER_NORMAL);
		setAniByFile(obj,
		"character/swordman/atanimation/char/testamentendbody_body.ani");
	}
}


function onProc_TestMent(obj)
{

	local state = obj.getSkillSubState();
	if (state == 0)
	{
		local state = obj.getSkillSubState();
		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);
		
	}else if (state == 1)
	{
		local state = obj.getSkillSubState();
		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);
		
		
		if (frmIndex >= 92 && obj.getVar("flag").get_vector(0) == 0)
		{
			obj.getVar("flag").set_vector(0,1);
			CreateTestaMentBodyCoverEffect(obj,0,-1,0);
		
		}
		
		if (frmIndex >= 51 && obj.getVar("flag").get_vector(1) == 0)
		{
			obj.getVar("flag").set_vector(1,1);
			//`BlackSwordAttack`
		}
		
		if (frmIndex >= 50 && obj.getVar("flag").get_vector(2) == 0)
		{
			obj.getVar("flag").set_vector(2,1);
			obj.sq_SetShake(obj, 20, 300);
			//`WhiteSwordAttack`
			local power = obj.sq_GetPowerWithPassive(SKILL_TESTAMENT , STATE_TESTAMENT , 2, -1, 1.0);
			sq_createAttackObjectWithPath(obj, "passiveobject/zrr_skill/atswordman/animation/attestament/finishattack.ani",
			"passiveobject/zrr_skill/atswordman/attackinfo/finishattack.atk",false,power,100,0,1,0);		

		}
		
		if (frmIndex >= 13 && obj.getVar("flag").get_vector(3) == 0)
		{
			obj.getVar("flag").set_vector(3,1);
			//`WhiteSwordAttack`
			local power = obj.sq_GetPowerWithPassive(SKILL_TESTAMENT , STATE_TESTAMENT , 0, -1, 1.0);
			sq_createAttackObjectWithPath(obj, "passiveobject/zrr_skill/atswordman/animation/attestament/finishattack.ani",
			"passiveobject/zrr_skill/atswordman/attackinfo/finishattack.atk",false,power,100,0,1,0);	
		}
		
		if (frmIndex >= 81 && obj.getVar("flag").get_vector(4) == 0)
		{
			obj.getVar("flag").set_vector(4,1);
			obj.sq_SetShake(obj, 30, 500);
			local power = obj.sq_GetPowerWithPassive(SKILL_TESTAMENT , STATE_TESTAMENT , 3, -1, 1.0);
			sq_createAttackObjectWithPath(obj, "passiveobject/zrr_skill/atswordman/animation/attestament/finishattack.ani",
			"passiveobject/zrr_skill/atswordman/attackinfo/finishattack.atk",false,power,100,0,1,0);	
			


			//`WhiteSwordAttack`
		}
		
		if (frmIndex >= 1 && obj.getVar("flag").get_vector(5) == 0)
		{
			obj.getVar("flag").set_vector(5,1);
			
			local target = findOneTarget(obj);
			if (target)
			{
				
				
				local masterAppendage = CNSquirrelAppendage.sq_AppendAppendage(target,
				obj, 14, false, 
				"character/atswordman/testment/ap_testment2.nut", true);	
				sq_HoldAndDelayDie(target, obj, false, true, true, 200, 200, ENUM_DIRECTION_NEUTRAL , masterAppendage);

				local masterAppendage = CNSquirrelAppendage.sq_AppendAppendage(target,
				obj, 14, false, 
				"character/atswordman/testment/ap_testment.nut", true);	

			}
			
		}
		
		if (frmIndex >= 1 && obj.getVar("flag").get_vector(6) == 0)
		{
			obj.getVar("flag").set_vector(6,1);
			
			local target = findOneTarget(obj);
			if (target)
			{
				sq_ApplayObjectAni(obj,target,STATE_TESTAMENT,obj.getXPos(),obj.getYPos(),1,ENUM_DRAWLAYER_COVER,9);
			}



		}
		
		
		if (frmIndex >= 2 && obj.getVar("flag").get_vector(13) == 0)
		{
			obj.getVar("flag").set_vector(13,1);
			
			local cover = obj.getVar("pooledObjToCover").get_obj_vector(1);
			if (cover)
				sq_ChangeDrawLayer(cover, ENUM_DRAWLAYER_COVER);
			
			
			cover = obj.getVar("pooledObjToCover").get_obj_vector(0);
			if (cover)
				sq_ChangeDrawLayer(cover, ENUM_DRAWLAYER_COVER);
			
			sq_ChangeDrawLayer(obj, ENUM_DRAWLAYER_COVER);



		}
		
		if (frmIndex >= 30 && obj.getVar("flag").get_vector(7) == 0)
		{
			obj.getVar("flag").set_vector(7,1);
			local ani = sq_GetObjectAniShadow(obj);
			if (ani)
				sq_SetfRotateAngle(ani, sq_ToRadian(70.0 ) );
				
			
			if (obj.getVar("soundRandom").get_vector(0) < 50)
				playSoundTime(obj,"SW_TESTAMENT_02_A",100);
			else
				playSoundTime(obj,"SW_TESTAMENT_02_B",100);

		}
		
		if (frmIndex >= 34 && obj.getVar("flag").get_vector(8) == 0)
		{
			obj.getVar("flag").set_vector(8,1);
			local ani = sq_GetObjectAniShadow(obj);
			if (ani)
				sq_SetfRotateAngle(ani, sq_ToRadian(60.0 ) );
				
			
		}
		
		if (frmIndex >= 36 && obj.getVar("flag").get_vector(9) == 0)
		{
			obj.getVar("flag").set_vector(9,1);
			local ani = sq_GetObjectAniShadow(obj);
			if (ani)
				sq_SetfRotateAngle(ani, sq_ToRadian(20.0 ) );
				
			
		}
		
		if (frmIndex >= 49 && obj.getVar("flag").get_vector(10) == 0)
		{
			obj.getVar("flag").set_vector(10,1);
			local ani = sq_GetObjectAniShadow(obj);
			if (ani)
				sq_SetfRotateAngle(ani, sq_ToRadian(17.0 ) );
				
		}
		
		if (frmIndex >= 52 && obj.getVar("flag").get_vector(11) == 0)
		{
			obj.getVar("flag").set_vector(11,1);
			local ani = sq_GetObjectAniShadow(obj);
			if (ani)
				sq_SetfRotateAngle(ani, sq_ToRadian(-5.0 ) );
			
		}
		
		if (frmIndex >= 83 && obj.getVar("flag").get_vector(12) == 0)
		{
			obj.getVar("flag").set_vector(12,1);

			if (obj.getVar("soundRandom").get_vector(0) < 50)
				playSoundTime(obj,"SW_TESTAMENT_03_A",100);
			else
				playSoundTime(obj,"SW_TESTAMENT_03_B",100);


			//`WhiteSwordAttack`
		}
		if (frmIndex >= 83)
		{
			if (sq_GetObjectAniShadow(obj))
				sq_SendShadowDestroy(obj);
		}
		{
			
			
			local startFrameIndex = 14;
			local endFrameIndex = 20;
			
			local startFrameIndex2 = 25;
			local endFrameIndex2 = 30;
			
			
			
			local maxSize = 900;
			local minSize = 75;
			
			local size = minSize;
			
		
			local currentT = sq_GetCurrentTime(pAni);
			if (frmIndex <= startFrameIndex)
			{
			
			}else if (frmIndex >= startFrameIndex && frmIndex <= endFrameIndex)
			{
		
				currentT = currentT - pAni.getDelaySum(0,startFrameIndex);
				local maxT = pAni.getDelaySum(startFrameIndex,endFrameIndex);
			
				if (currentT < maxT)
				{

					local currentAni = sq_GetCurrentAnimation(obj);
					size = sq_GetAccel(minSize, maxSize, currentT,maxT, true);
					

				}
			}else if (frmIndex < startFrameIndex2)
			{
				size = maxSize;
			}
			
			
			
			
			local startFrameIndex = startFrameIndex2;
			local endFrameIndex = endFrameIndex2;
		
			local currentT = sq_GetCurrentTime(pAni);
			if (frmIndex <= startFrameIndex)
			{

			}else if (frmIndex >= startFrameIndex && frmIndex <= endFrameIndex)
			{
		
				currentT = currentT - pAni.getDelaySum(0,startFrameIndex);
				local maxT = pAni.getDelaySum(startFrameIndex,endFrameIndex);
			
				if (currentT < maxT)
				{
					
					local currentAni = sq_GetCurrentAnimation(obj);
					size = sq_GetAccel(maxSize, minSize, currentT,maxT, true);
					

				}
			}else{
				size = minSize;
			}
			
			
			
			local sizeRate = size.tofloat() / 100.0;
			local var = obj.getVar("pooledObj");  		
			local objectsSize = var.get_obj_vector_size();

			for(local i=0;i<objectsSize;++i)
			{
				local damager = var.get_obj_vector(i);
				if (damager)
				{
				
					local currentAni = sq_GetCurrentAnimation(damager);
					currentAni.setImageRateFromOriginal(sizeRate, sizeRate);

				}
			}
			
			local currentAni = sq_GetCurrentAnimation(obj);
			currentAni.setImageRateFromOriginal(sizeRate - 0.1, sizeRate - 0.1);
			local ani = sq_GetObjectAniShadow(obj);
			if (ani)
				ani.setImageRateFromOriginal(sizeRate, sizeRate);
			
		
		}
	
	

		
	}


}

function onEndState_TestMent(obj,new_state)
{

	if (new_state != STATE_TESTAMENT && new_state != STATE_STAND)
		setVliadEffectTestamentPassiveobject(obj);


	if (new_state != STATE_TESTAMENT )
	{
		
		sq_SetCharacterNameHide(obj,false);
		sq_ChangeDrawLayer(obj, ENUM_DRAWLAYER_NORMAL);


		local currentAni = sq_GetCurrentAnimation(obj);
		currentAni.setImageRateFromOriginal(1.0, 1.0);
		
		local ani = sq_GetObjectAniShadow(obj);
		if (ani)
		{
			ani.setImageRateFromOriginal(1.0, 1.0);
			sq_SetfRotateAngle(ani, sq_ToRadian(0.0 ) );
		}
	}
}


function setVliadEffectTestamentPassiveobject(obj)
{
	local var = obj.getVar("pooledObj");  		
	local objectsSize = var.get_obj_vector_size();

	for(local i=0;i<objectsSize;++i)
	{
		local effectObj = var.get_obj_vector(i);
		if (effectObj)
		{
//			sq_SendCreatePassiveObjectPacketPos(obj,24357, 0, obj.getXPos(), obj.getYPos(), 0 );
			effectObj.setValid(false);
		}
	}

}


function onEndCurrentAni_TestMent(obj)
{
	local state = obj.getSkillSubState();

	if (state == 0)
	{
		local pIntVec = sq_GetGlobalIntVector();
		sq_IntVectorClear(pIntVec);
		sq_IntVectorPush(pIntVec, 1);			
		obj.addSetStatePacket(STATE_TESTAMENT , pIntVec,  STATE_PRIORITY_USER, false, "");
	}else if (state == 1)
	{
		local pIntVec = sq_GetGlobalIntVector();
		sq_IntVectorClear(pIntVec);
		sq_IntVectorPush(pIntVec, 2);			
		obj.addSetStatePacket(STATE_TESTAMENT , pIntVec,  STATE_PRIORITY_USER, false, "");
	}else if (state == 2)
	{
		sq_ChangeDrawLayer(obj, ENUM_DRAWLAYER_NORMAL);

		if (obj.getVar("soundRandom").get_vector(0) < 50)
			playSoundTime(obj,"SW_TESTAMENT_04_A",100);
		else
			playSoundTime(obj,"SW_TESTAMENT_04_B",100);


		local pIntVec = sq_GetGlobalIntVector();
		sq_IntVectorClear(pIntVec);
		sq_IntVectorPush(pIntVec, 2);			
		obj.addSetStatePacket(0 , pIntVec,  STATE_PRIORITY_USER, false, "");
		sq_SendMessage(obj,OBJECT_MESSAGE_INVINCIBLE,1,0);
		sq_PostDelayedMessage(obj,OBJECT_MESSAGE_INVINCIBLE,0,0,500 );
		
	}
}




function CreateTestaMentStartBodyEffect(obj,x,y,z)
{
	if(!obj.isMyControlObject())
		return ;
	local ani = sq_CreateAnimation("","character/swordman/effect/animation/attestament/testamentstart_bodyeffect.ani");
	ani.setSpeedRate(100.0 );
	local pooledObj = sq_CreatePooledObject(ani,true);
	//pooledObj = sq_SetEnumDrawLayer(pooledObj, ENUM_DRAWLAYER_COVER);
	local posX = sq_GetDistancePos(obj.getXPos(), obj.getDirection(), x);
	pooledObj.setCurrentPos(posX ,obj.getYPos() + y,obj.getZPos() + z);
	pooledObj.setCurrentDirection(obj.getDirection());
	sq_AddObject(obj,pooledObj,2,false);	
	
	obj.getVar("pooledObj").push_obj_vector( pooledObj );

	

}

function CreateTestaMentBodyCoverEffect(obj,x,y,z)
{
	if(!obj.isMyControlObject())
		return ;
	local ani = sq_CreateAnimation("","character/swordman/effect/animation/attestament/testamentendcover_06.ani");
	ani.setSpeedRate(80.0 );
	local pooledObj = sq_CreatePooledObject(ani,true);
	pooledObj = sq_SetEnumDrawLayer(pooledObj, ENUM_DRAWLAYER_COVER);
	local posX = sq_GetDistancePos(obj.getXPos(), obj.getDirection(), x);
	pooledObj.setCurrentPos(posX ,obj.getYPos() + y,obj.getZPos() + z);
	pooledObj.setCurrentDirection(obj.getDirection());
	sq_AddObject(obj,pooledObj,2,false);	
	obj.getVar("pooledObj").push_obj_vector( pooledObj );
	obj.getVar("pooledObjToCover").push_obj_vector( pooledObj );

}


function CreateTestaMentBackGroundEffect(obj,x,y,z)
{
	if(!obj.isMyControlObject())
		return ;
	local ani = sq_CreateAnimation("","character/swordman/effect/animation/attestament/testamentbackground_00.ani");

	local pooledObj = sq_CreatePooledObject(ani,true);
	pooledObj = sq_SetEnumDrawLayer(pooledObj, ENUM_DRAWLAYER_COVER);
	local posX = sq_GetDistancePos(obj.getXPos(), obj.getDirection(), x);
	pooledObj.setCurrentPos(posX ,obj.getYPos() + y,obj.getZPos() + z);
	pooledObj.setCurrentDirection(obj.getDirection());
	sq_AddObject(obj,pooledObj,2,false);	
	obj.getVar("pooledObj").push_obj_vector( pooledObj );

}

function CreateTestaMentFrontBEffect(obj,x,y,z)
{
	if(!obj.isMyControlObject())
		return ;
	local ani = sq_CreateAnimation("","character/swordman/effect/animation/attestament/testamentfrontb_00.ani");

	local pooledObj = sq_CreatePooledObject(ani,true);
	pooledObj = sq_SetEnumDrawLayer(pooledObj, ENUM_DRAWLAYER_COVER);
	local posX = sq_GetDistancePos(obj.getXPos(), obj.getDirection(), x);
	pooledObj.setCurrentPos(posX ,obj.getYPos() + y,obj.getZPos() + z);
	pooledObj.setCurrentDirection(obj.getDirection());
	sq_AddObject(obj,pooledObj,2,false);	

	obj.getVar("pooledObj").push_obj_vector( pooledObj );
	obj.getVar("pooledObjToCover").push_obj_vector( pooledObj );

}
